/* 包含n个reducers函数 根据老的state和action返回一个新的state */

import { combineReducers } from 'redux'

import { DECREMENT, INCREMENT, ADD_COMMENT, DELETE_COMMENT, RECIVE_COMMENTS } from "./action-type";

// import { ADD_COMMENT, DELETE_COMMENT } from './action-type'


const initComments = []
// action 新的数据
// state 历史数据
// 用来返回新的数据 重新渲染页面
function comments(state = initComments, action) {
    switch (action.type) {
        case ADD_COMMENT:
            return [action.data, ...state]
        case DELETE_COMMENT:
            return state.filter((comment, index) => index !== action.data);
        case RECIVE_COMMENTS:
            return action.data
        default:
            return state
    }
}

function counter(state = 0, action) {
    console.log('counter', state, action)

    switch (action.type) {
        case INCREMENT:
            return state + action.data
        case DECREMENT:
            return state - action.data
        default:
            return state
    }
}



//redux 向外暴露的是什么结构
export default combineReducers(
    { comments, counter }
)
